﻿using ESMAM.Model.SYS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Collections.Specialized;

namespace ESMAM.DAL.SYS
{
    public class UserDAL : ESMAM.DAL.DAO.DAL
    {

        public sys_user Login(string loginName, string password)
        {
            return DbTop.Get<sys_user>("sys_user", new { loginname = loginName, passwd = password });
        }

        public bool ChangePassWd(string id, string password)
        {

            string updateSql = "update SYS_USER set passwd=@passwd where ID=@id ";

            return DbTop.ExecSql(updateSql, new { passwd = password, id = id.ToString() }) > 0;
        }

        public Pager GetPager(ReqParam req)
        {
            var kv = DbTop.GetDataTableByPager(req.CurrentPage, req.PageSize, "*", "sys_user ", req.Where, req.Orders);             
            Pager pager = new Pager()
            {
                list = kv.Key,
                pageCurrent = req.CurrentPage,
                pageSize = req.PageSize,
                totalRow = kv.Value
            };
            return pager;
        }

        public DataTable SelectTable(ReqParam req)
        {
            return DbTop.SelectTable("sys_user ", req.Where, req.Orders);
        }

        public KeyValuePair<bool,string> Upsert(NameValueCollection nvc)
        {
            var kv = DbTop.Save(nvc, "sys_user", "id");

            string msg = string.Empty;

            if (kv.Key == MJTop.Data.SaveType.Insert)
            {
                if (kv.Value)
                {
                    msg = "添加成功！";
                }
                else
                {
                    msg = "添加失败！";
                }
            }
            else
            {
                if (kv.Value)
                {
                    msg = "更新成功！";
                }
                else
                {
                    msg = "更新失败！";
                }
            }
            return new KeyValuePair<bool, string>(kv.Value, msg);
        }

        public sys_user GetUser(string id)
        {
            return DbTop.GetById<sys_user>("sys_user", id);
        }

        public int Delete(string id)
        {
            int res= DbTop.Delete("sys_user", id);
            return res;
        }
    }
}
